<!--
 * @Description: 平台标准任务全局配置弹窗
 * @Author: liyujie
 * @Date: 2021/10/27
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/10/27
-->
<template>
    <section class="wrapper">
        <el-dialog
            title="全局设置"
            :visible.sync="dialogVisible"
            width="680px"
            :close-on-click-modal="false"
            @close="$emit('close')"
        >
            <div class="dialog-content">
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="108px"
                    size="medium"
                    @submit.native.prevent
                >
                    <el-form-item
                        label="审核时间提示"
                        prop="audit_need_days"
                        class="is-required"
                    >
                        <el-input
                            v-model="ruleForm.audit_need_days"
                            clearable
                            style="width: 300px"
                            size="medium"
                            placeholder="请输入..."
                        >
                            <template slot="append">天</template>
                        </el-input>
                    </el-form-item>
                    <el-form-item
                        label="用户领取限制"
                        class="is-required"
                    >
                        <div
                            class="user-item"
                            v-for="(item, index) in ruleForm.student_level_limit_list"
                            :key="index"
                        >
                            <div class="title">E{{ item.agent_level }}：领取</div>
                            <el-form-item
                                :prop="`student_level_limit_list[${index}].limit_count`"
                                :rules="rules.limit_count"
                            >
                                <el-input
                                    placeholder="请输入内容"
                                    v-model="item.limit_count"
                                    size="medium"
                                >
                                    <el-select
                                        v-model="item.limit_type"
                                        slot="append"
                                        placeholder="请选择"
                                        style="width: 78px"
                                    >
                                        <el-option
                                            label="次/日"
                                            :value="1"
                                        />
                                        <el-option
                                            label="次/周"
                                            :value="2"
                                        />
                                    </el-select>
                                </el-input>
                            </el-form-item>
                        </div>
                    </el-form-item>
                </el-form>
            </div>
            <div
                class="dialog-footer"
                style="padding-top: 12px"
            >
                <el-button
                    size="medium"
                    style="width: 120px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    style="width: 120px;margin-left: 32px"
                    @click="handleConfirm()"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import {deepClone} from "@/assets/js/utils";
import Vue from "vue";

const Dialog = {
    name: "StandardConfigDialog",
    props: {
        type: {
            type: Number,
            default: 7
        }
    },
    filters: {},
    data() {
        return {
            dialogVisible: false,
            ruleForm: {
                audit_need_days: '',
                student_level_limit_list: [
                    {
                        agent_level: 0,
                        limit_type: 1,
                        limit_count: -1,
                    },
                    {
                        agent_level: 1,
                        limit_type: 1,
                        limit_count: -1,
                    },
                    {
                        agent_level: 2,
                        limit_type: 1,
                        limit_count: -1,
                    },
                    {
                        agent_level: 3,
                        limit_type: 1,
                        limit_count: -1,
                    },
                    {
                        agent_level: 4,
                        limit_type: 1,
                        limit_count: -1,
                    },
                    {
                        agent_level: 5,
                        limit_type: 1,
                        limit_count: -1,
                    }
                ]
            },
            rules: {
                audit_need_days: [
                    {required: true, message: '请填写审核天数', trigger: 'blur'},
                    {pattern: /^(0|[1-9][0-9]*|-[1-9][0-9]*)$/, message: '审核天数必须为整数', trigger: 'blur'}
                ],
                limit_count: [
                    {required: true, message: '请填写领取次数', trigger: 'blur'},
                    {pattern: /^(0|[1-9][0-9]*|-[1-9][0-9]*)$/, message: '领取次数必须为整数', trigger: 'blur'}
                ]
            },
        };
    },
    watch: {},
    mounted() {
        this.getConfig();
    },
    methods: {

        /**
         * 显示弹窗
         */
        show() {
            this.dialogVisible = true;
        },

        /**
         * 显示弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 获取数据
         */
        getConfig() {
            let url = this.type === 7 ? '/student/task_stuagent%5Ctask_attract_recommend%5Cget_config' : '/student/task_stuagent%5Ctask_questionnaire%5Cget_config';

            this.$post(url, {}, resp => {
                if (resp.code === 1) {
                    this.ruleForm = resp.data;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 点击确定
         */
        handleConfirm() {
            this.$refs.ruleForm.validate((valid) => {
                if(valid) {
                    let params = deepClone(this.ruleForm);
                    let url = this.type === 7 ? '/student/task_stuagent%5Ctask_attract_recommend%5Cset_config' : '/student/task_stuagent%5Ctask_questionnaire%5Cset_config';

                    params.student_level_limit_list_json = JSON.stringify(params.student_level_limit_list);
                    delete params.student_level_limit_list;
                    this.$post(url, params, resp => {
                        if (resp.code === 1) {
                            this.$notify({
                                title: '提示',
                                message: '保存成功',
                                duration: 2000,
                                type: 'success'
                            });
                            this.$emit('confirm', true);
                        } else {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                duration: 2000,
                                type: 'warning'
                            });
                        }
                    });
                }
            });
        },
    }
};

export default Dialog;

const standardConfigDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", function () {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", function (data) {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export {standardConfigDialog};
</script>

<style scoped lang="less">
.wrapper {
    .dialog-content {
        display: flex;
        flex-direction: column;
        padding: 0 32px;
        .user-item {
            width: 300px;
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            .title {
                width: 78px;
                margin-right: 12px;
            }
        }
    }

    .dialog-footer {
        display: flex;
        justify-content: center;
    }
}

/deep/ .el-dialog__header {
    padding-top: 10px;
    box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
    display: flex;
    align-items: center;
    background: #FAFAFA;
    border-radius: 4px 4px 0 0;

    .el-dialog__title {
        font-weight: 500;
        color: #333;
        font-size: 14px;
    }

    .el-icon-close:before {
        color: rgb(194, 194, 194);
        font-size: 17px;
        font-weight: bold;
    }

    .el-dialog__headerbtn {
        top: 14px;
    }
}

/deep/ .el-dialog__body {
    padding: 24px 32px;
}

/deep/ .el-textarea__inner {
    resize: none;
}

/deep/ .el-input-number .el-input__inner {
    text-align: left;
}

/deep/ .el-dialog {
    margin-top: 10vh !important;
}

.is-required {
    .el-form-item__label {
        &:before {
            content: "*";
            display: inline-block;
            color: #F56C6C;
        }
    }
}

.link-text {
    color: #20A0FF;
    cursor: pointer;
    text-overflow: ellipsis;
    overflow: hidden;
}
</style>
